import {
  loanItemList, loanManualTransfer, loanManualTransferSuccess,
  loanManualTransferFail, addLoanRepayItem, addLoanRepayItemClear, queryRepayItemByLoanId, loanManualTransferYb,
  xbeautyBatchWithdraw, repayedLoanCount, waitRepayLoanCount, waitLendLoanCount, lendFailLoanCount,
  postWithhold, xbeautySmartBatchWith,
  getAutoTransferStatus,
  postAutoTransferSwitch,
} from '../../services/order/api';

export default {
  namespace: 'loanBill',

  state: {
    data: [],
    pagination: {
    },
    loading: true,
    operatorLoading: false,
    selectItems: {
      sourceSelectItems: [
        { name: '想美', value: 'xbeauty' },
        { name: '信花花', value: 'xflower' },
      ],
    },
    userSelect: [],

    billItemLoading: false,
    repayItemLoading: false,
    statisticsData:{},
    currRepayItem: [],
  },

  effects: {
    *fetch({ payload }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(loanItemList, payload);
      if (response && response.code == 200 && response.data && response.data.records) {
        response.data.records.map(data => {
          data['key'] = data.loanId;
        });
        const r = {
          data: response.data.records,
          pagination: {
            total: response.data.total,
            pageSize: payload.pageSize,
            current: payload.current,
          },
        };
        yield put({
          type: 'save',
          payload: r,
        });
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *loanManualTransfer({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(loanManualTransfer, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *loanManualTransferYb({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(loanManualTransferYb, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *loanManualTransferFail({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(loanManualTransferFail, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *loanManualTransferSuccess({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(loanManualTransferSuccess, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *queryRepayItemByLoanId({ payload }, { call, put, select }) {
      yield put({
        type: 'changeItemLoading',
        payload: { repayItemLoading: true },
      });
      const response = yield call(queryRepayItemByLoanId, payload);
      if (response && response.code == 200 && response.data && response.data.length > 0) {
        response.data.map((item, index) => {
          item['key'] = item.id;
          return item;
        });

        const r = {
          data: response.data,
        };
        yield put({
          type: 'saveCurrRepayItem',
          payload: r,
        });
      }
      yield put({
        type: 'changeItemLoading',
        payload: { repayItemLoading: false },
      });
    },

    *addLoanRepayItem({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addLoanRepayItem, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *addLoanRepayItemClear({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addLoanRepayItemClear, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *xbeautyBatchWithdraw({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(xbeautyBatchWithdraw, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *xbeautySmartBatchWith({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(xbeautySmartBatchWith, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },


    *fetchSelectItems({ payload }, { call, put }) {
      const response = yield call(queryEntity, payload, payload.service, payload.apiUrl);
      if (response && response.code == 200) {
        response.data.records.map(data => data['key'] = data.id);
        const r = {};
        r[payload.selectName] = response.data.records;
        yield put({
          type: 'saveSelectItems',
          payload: r,
        });
      }
    },
    *fetchStatistics({ payload ,callback}, { call, put }) {
      const response = yield call(waitLendLoanCount, payload);

      yield put({
        type: 'saveStatistic',
        payload: {
          statisticsData:response.data
        },
      });

    },
    *fetchLendFailLoanCount({ payload ,callback}, { call, put }) {
      const response = yield call(lendFailLoanCount, payload);

      yield put({
        type: 'saveStatistic',
        payload: {
          statisticsData:response.data
        },
      });

    },

    *fetchHaveRepayedLoanCount({ payload ,callback}, { call, put }) {
      const response = yield call(repayedLoanCount, payload);

      yield put({
        type: 'saveStatistic',
        payload: {
          statisticsData:response.data
        },
      });

    },

    *fetchWaitRepayLoanCount({ payload ,callback}, { call, put }) {
      const response = yield call(waitRepayLoanCount, payload);

      yield put({
        type: 'saveStatistic',
        payload: {
          statisticsData:response.data
        },
      });

    },

    *fetchPostWithhold({ payload ,callback}, { call, put }) {
      const response = yield call(postWithhold, payload);

      yield put({
        type: 'saveStatistic',
        payload: {
          postWithholdRes:response.data
        },
      });
      
      if (callback) yield callback(response.code, response.message);

    },

    *fetchGetAutoTransferStatus({ payload ,callback}, { call, put }) {
      const response = yield call(getAutoTransferStatus, payload);

      yield put({
        type: 'saveAutoTransferStatus',
        payload: {
          getAutoTransferStatusData:response.data
        },
      });

      if (callback) yield callback(response);
    },

    *fetchPostAutoTransferSwitch({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(postAutoTransferSwitch, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
        pagination: action.payload.pagination,
      };
    },
    saveCurrBillItem(state, action) {
      return {
        ...state,
        currBillItem: action.payload.data,
      };
    },
    saveStatistic(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    },
    saveCurrRepayItem(state, action) {
      return {
        ...state,
        currRepayItem: action.payload.data,
      };
    },
    changeItemLoading(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changeOperatorLoading(state, action) {
      return {
        ...state,
        operatorLoading: action.payload,
      };
    },
    saveSelectItems(state, action) {
      return {
        ...state,
        selectItems: {
          ...state.selectItems,
          ...action.payload,
        },
      }
    },
    saveAutoTransferStatus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    }
  },

};
